[ty] provide import completion when in from <name> <name> statement#21291
Conversation
from <name> <name> statement
Diagnostic diff on typing conformance testsNo changes detected when running ty on typing conformance tests ✅ |
|
from <name> <name> statementimport completion when in from <name> <name> statement
import completion when in from <name> <name> statementimport completion when in from <name> <name> statement
MichaReiser
left a comment
There was a problem hiding this comment.
Only showing import in a from <NAME> <NAME2> position does make sense to me. I was surprised to see that Pylance shows all completions, but gives import a higher ranking
|
I just checked out this PR and built the playground and this works as expected when you have This could either be because the playground applies its own filtering (not sure if there's a way for us to disable it, do you want to take a look) or because our own "text prefix matching" |
|
Yeah we may not be able to fully control whether |
|
Thank you, I will revisit this in a few days, when I am back home. |
crates/ty_ide/src/completion.rs
Outdated
| return None; | ||
| } | ||
|
|
||
| let from_index = tokens.iter().position(|t| t.kind() == TokenKind::From)?; |
There was a problem hiding this comment.
The search here seems to be too permissive to me. What's preventing it from going across logical lines?
Can you add some tests with multiple statements? I suspect that we now incorrectly match on import completions if there's any import statement before <CURSOR>.
There was a problem hiding this comment.
Good point, will add that
crates/ty_ide/src/completion.rs
Outdated
| return None; | ||
| } | ||
|
|
||
| let from_index = tokens.iter().rposition(|t| t.kind() == TokenKind::From)?; |
There was a problem hiding this comment.
I still think this is incorrect as it cross logical line boundaries. E.g. what if
from module impo
a<CURSOR>We would want to show completions for a and not suggest import
There was a problem hiding this comment.
I haven't checked out the PR. What I'm concerned with the current approach is that tokens is anything before <CURSOR> (unless this isn't the case?). That means, it's possible for tokens.iter().rposition() to find the from even though it belongs to an entirely different statement.
crates/ty_ide/src/completion.rs
Outdated
| // If we have `from .('.' | '...')* <name>` we can suggest `import`. | ||
| // We check that we must have at least one `.` because we don't want to suggest `import` for `from <name>`. | ||
| // | ||
| // If we have `from ('.' | '...')* dotted_name <name>` we can suggest `import`. |
There was a problem hiding this comment.
Can you make sure comments are wrapped to 99 columns (inclusive) please? There are some other comments below that need to be wrapped too.
crates/ty_ide/src/completion.rs
Outdated
| } | ||
|
|
||
| None | ||
| } |
There was a problem hiding this comment.
I think this routine should be modeled on this existing function:
ruff/crates/ty_ide/src/completion.rs
Lines 667 to 756 in f44598d
Maybe there is a way to factor out the common logic, but even if there isn't, just copying the approach seems like the right move here. I agree with @MichaReiser above. And notice that the function above is careful to not do too much look-behind on the token sequence. It also handles things like ellipsis, non-logical newlines and so on.
crates/ty_ide/src/completion.rs
Outdated
| #[test] | ||
| fn from_import_two_imports_suggests_import() { | ||
| let builder = | ||
| completion_test_builder("from collections.abc import Sequence\nfrom typing i<CURSOR>"); |
There was a problem hiding this comment.
For something really short, I'm fine with the \n here. But this starts to make very long lines. I'd rather see this written out using actual new lines:
let builder = completion_test_builder(
"from collections.abc import Sequence
from typing i<CURSOR>"
);
The test code should automatically handle dedenting.
|
@MatthewMckee4 I'm going to take over this PR since we'd like to get this fixed. And I'm planning to work on more stuff around keyword completions this week, which will really want to build on top of this PR. Thank you. :-) |
|
Cool, apologies for the delays, I've been a little busy. Thank you for the feedback too! |
never feel like you need to apologise for unpaid volunteer work :-) we really appreciate the contribution!! |
8a98b47 to
446048d
Compare
This works principally by copying `from_import_tokens` and tweaking it a bit to look for `from module <CURSOR>` token sequences. We actually wind up needing to be a little more careful here to parse `<module>` correctly to avoid something like `from os.i<CURSOR>` from being treated as if the `i` is the beginning of the `import` keyword. Fixes astral-sh/ty#1494
446048d to
8daeed3
Compare
* origin/main: (38 commits) [ty] Make implicit submodule imports only occur in global scope (#21370) [ty] introduce local variables for `from` imports of submodules in `__init__.py(i)` (#21173) [`ruff`] Ignore `str()` when not used for simple conversion (`RUF065`) (#21330) [ty] implement `typing.NewType` by adding `Type::NewTypeInstance` [ty] supress inlay hints for `+1` and `-1` (#21368) [ty] Use type context for inference of generic constructors (#20933) [ty] Improve generic call expression inference (#21210) [ty] supress some trivial expr inlay hints (#21367) [`configuration`] Fix unclear error messages for line-length values exceeding `u16::MAX` (#21329) [ty] Fix incorrect inference of `enum.auto()` for enums with non-`int` mixins, and imprecise inference of `enum.auto()` for single-member enums (#20541) [`refurb`] Detect empty f-strings (`FURB105`) (#21348) [ty] provide `import` completion when in `from <name> <name>` statement (#21291) [ty] elide redundant inlay hints for function args (#21365) Fix syntax error false positive on alternative `match` patterns (#21362) Add a new "Opening a PR" section to the contribution guide (#21298) [`flake8-simplify`] Fix SIM222 false positive for `tuple(generator) or None` (`SIM222`) (#21187) Rebuild ruff binary instead of sharing it across jobs (#21361) [ty] Fix `--exclude` and `src.exclude` merging (#21341) [ty] Add support for properties that return `Self` (#21335) Add upstream linter URL to `ruff linter --output-format=json` (#21316) ...
Ideally this would have been added as part of #21291, but I forgot.
Ideally this would have been added as part of #21291, but I forgot.


Summary
Resolves astral-sh/ty#1494
Test Plan
Add a test showing if we are in
from <name> <name>we provide the keyword completion "import"